package com.wbao.dianniu.ui;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.KnowListAdapter;
import com.wbao.dianniu.data.KnowledgeData;
import com.wbao.dianniu.db.KnowDbHelper;
import com.wbao.dianniu.listener.IKnowledgeListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.KnowListManager;
import com.wbao.dianniu.update.KnowledgeHelper;
import com.wbao.dianniu.update.ErrorDialogHelper;
import com.wbao.dianniu.utils.ActivityUtils;
import com.wbao.dianniu.utils.Notification;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的文章
 *
 * Created by Administrator on 2017/8/30.
 */

public class MyKnowledgeActivity extends BaseActivity implements IKnowledgeListListener, KnowledgeHelper.IKnowledgeChangedListener {

    private Button rightBtn;

    private PullToRefreshListView pullListView;
    private ListView listView;
    private final int PAGE_COUNTS = 10;
    private int currentPage = 0;
    private KnowListManager manager;
//    private String searchKey = null;
    private KnowListAdapter adapter;
    private final int TYPE = 2;
    private Dialog dialog;
    private TextView noDataTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_my_knowledge, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.my_knowledge));
        initView();
        initData();
        requestData(currentPage);
    }

    private void initView() {
        rightBtn = getRightTextButton();
        rightBtn.setOnClickListener(this);

        pullListView = findViewById(R.id.my_knowledge_listview);
        pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
        pullListView.setOnRefreshListener(onRefreshListener);
        listView = pullListView.getRefreshableView();
        registerForContextMenu(listView);

        noDataTV = findViewById(R.id.no_data_tv);
    }

    private void initData() {
        manager = new KnowListManager(this);
        manager.addKnowledgeListListener(this);
        adapter = new KnowListAdapter(this, 2);
        adapter.addData(new ArrayList<KnowledgeData>());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(itemClickListener);

        KnowledgeHelper.getInstance().addListener(this);
    }

    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            KnowledgeData data = (KnowledgeData) adapter.getItem(position - 1);
            ActivityUtils.intoKnowDetailActivity(MyKnowledgeActivity.this, 0, data);
        }
    };

    PullToRefreshBase.OnRefreshListener onRefreshListener = new PullToRefreshBase.OnRefreshListener() {

        @Override
        public void onRefresh() {
            //  上拉刷新 加载请求数据
            pullUpRequest();
        }
    };

    // 上拉刷新数据请求
    private void pullUpRequest() {
        //没有搜索结果 或者数据为空 上拉没有效果
        currentPage++;
        requestData(currentPage);
    }

    private void requestData(int page) {
        manager.knowledgeList(GlobalContext.getAccountId(), TYPE, null, page * PAGE_COUNTS, PAGE_COUNTS);
    }

    @Override
    public void onKnowledgeListFailure(int errorCode, String errorMsg) {
        pullListView.onRefreshComplete(false, false);
        Notification.toast(this, errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onKnowledgeListSuccess(List<KnowledgeData> list) {
        pullListView.onRefreshComplete(false, false);
        if (null != list) {
            adapter.addData(list);
            if (adapter.getCount() == 0) {
                noDataTV.setVisibility(View.VISIBLE);
            } else {
                noDataTV.setVisibility(View.GONE);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.right_button:
                if(!GlobalContext.getPerfectData()){
                    ErrorDialogHelper.getInstance().showPerfectAuth(this);
                    return;
                }
                Intent intent = new Intent();
                intent.setClass(MyKnowledgeActivity.this, CreateKnowActivity.class);
                startActivity(intent);
                /*Intent intent = new Intent();
                intent.setClass(MyKnowledgeActivity.this, NewActivity.class);
                startActivity(intent);*/
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager)
            manager.removeKnowledgeListListener(this);
        KnowledgeHelper.getInstance().removeListener(this);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        KnowDbHelper dbHelper = new KnowDbHelper(this);
        if(dbHelper.getStatus(GlobalContext.getAccountId())){
            rightBtn.setText(getResources().getString(R.string.draft_box));
        }else{
            rightBtn.setText(getResources().getString(R.string.write_knowledge));
        }

    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void addOneKnowledge(KnowledgeData data) {
        adapter.addFirstData(data);
        if (adapter.getCount() == 0) {
            noDataTV.setVisibility(View.VISIBLE);
        } else {
            noDataTV.setVisibility(View.GONE);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void removeOneKnowledge(int knowledgeId) {
        adapter.removeOneData(knowledgeId);
        if (adapter.getCount() == 0) {
            noDataTV.setVisibility(View.VISIBLE);
        } else {
            noDataTV.setVisibility(View.GONE);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void goodNotify(int knowledgeId, int isGood, int goodCount) {
        adapter.goodChanged(knowledgeId, isGood, goodCount);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void replyNotify(int knowledgeId, int answerCounts) {
        adapter.replyNotify(knowledgeId, answerCounts);
    }

    @Override
    public void visitorNotify(int knowledgeId, int visitorCounts) {
        adapter.visitorNotify(knowledgeId, visitorCounts);
    }

    @Override
    public void modifyOneData(int knowledgeId, KnowledgeData data) {
        adapter.modifyOneData(knowledgeId,data);
    }


}
